package com.freeman.cement.controller;

import com.freeman.cement.annotation.NeedRole;
import com.freeman.cement.entity.ResponseResult;
import com.freeman.cement.entity.Staff;
import com.freeman.cement.exception.AppException;
import com.freeman.cement.exception.AppExceptionCodeMsg;
import com.freeman.cement.service.StaffService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 66
 */
@RestController
@RequestMapping("/staff")
@CrossOrigin    //允许跨域访问
public class StaffController {

    @Resource
    private StaffService service;

    /**
     * 检索人员
     * @param staff
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/selectStaff", method = RequestMethod.POST)
    public ResponseResult selectStaff(Staff staff, @RequestParam int pageNo, @RequestParam int pageSize) {
        return service.selectStaff(staff, pageNo, pageSize);
    }

    /**
     * 通过关键字检索人员，根据人员姓名和手机号关键字检索
     * @param keyword 姓名、手机号关键字
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/selectStaffByKeyword", method = RequestMethod.POST)
    public ResponseResult selectStaffByKeyword(String keyword, @RequestParam int pageNo, @RequestParam int pageSize){
        return service.selectStaffByKeyword(keyword, pageNo, pageSize);
    }

    /**
     * 人员添加
     * @param staff
     * @return
     */
    @NeedRole("管理员")
    @RequestMapping(value = "/addStaff", method = RequestMethod.POST)
    public ResponseResult addStaff(Staff staff) {
        return service.addStaff(staff);
    }

    /**
     * 人员删除
     * @param id
     * @return
     */
    @NeedRole("管理员")
    @RequestMapping(value = "/deleteStaff", method = RequestMethod.POST)
    public ResponseResult deleteStaff(@RequestParam("id") int id) {
        return service.deleteStaff(id);
    }

    /**
     * 人员信息修改
     * @param staff
     * @return
     */
    @NeedRole("管理员")
    @RequestMapping(value = "/updateStaff", method = RequestMethod.POST)
    public ResponseResult updateStaff(Staff staff) {
        return service.updateStaff(staff);
    }

    /**
     * 目前未使用 66 20230222
     * 用户自己修改自己的密码，需要用户知道自己的原始密码
     * @param id
     * @param oldPassword
     * @param newPassword
     * @return
     */
    @NeedRole("管理员||值班员||处置员")
    @RequestMapping(value = "/editPasswordByStaff", method = RequestMethod.POST)
    public ResponseResult editPasswordByStaff(@RequestParam("id") int id, @RequestParam("oldPassword") String oldPassword, @RequestParam("newPassword") String newPassword){
        return service.editPasswordByStaff(id, oldPassword, newPassword);
    }

    /**
     * 管理员修改用户的密码
     * @param id
     * @param newPassword
     * @return
     */
    @NeedRole("管理员")
    @RequestMapping(value = "/editPassword", method = RequestMethod.POST)
    public ResponseResult editPassword(@RequestParam("id") int id, @RequestParam("newPassword") String newPassword){
        return service.editPasswordByAdmin(id, newPassword);
    }

}
